import { observable, action,toJS } from 'mobx';
import { ListViewPage } from 'weaver-mobile-page';
import * as  Apis from '../../apis/govern';
/*
 * @info: OpinionStore
 * @Author: lusx
 * @Date: 2019-12-01 11:13:16
 * @Last modified by:   lusx
 * @Last modified time: 01-04-2019 01:51:19
 */
export default class OpinionStore {
  @observable listViewStore = new ListViewPage.Store({ route: '/pro/duty/info/opinion' });
  @observable listViewCommentStore = new ListViewPage.Store({ route: '/pro/duty/info/opinion/comment' });
  @observable isGetCommentData = false;//是否获取评论child数据
  @observable commentDataId = '';
  @observable newArray = [];
  cache = {};
  @observable instructTopid = "";
  @observable instructid = "";
  constructor(props) {
    this.initDatas = this.initDatas.bind(this);
    this.getInstructList = this.getInstructList.bind(this);
    this.onEndReached = this.onEndReached.bind(this);
    this.onRefresh = this.onRefresh.bind(this);
    this.initComment = this.initComment.bind(this);
    this.getInstructListComment = this.getInstructListComment.bind(this);
    this.clearState = this.clearState.bind(this);
    this.setInstructTopid = this.setInstructTopid.bind(this);
    this.setInstructid = this.setInstructid.bind(this);
  }
  /**
   * 初始化方法
   * @method initDatas
   * @for InfoStore
   * @param {参数类型} {'pageindex':num, 'pagesize':num,  'taskId': 'num' } 参数说明 ;
   * @return {返回值类型}
   */
  @action
  initDatas(params,callback=false){
    this.getInstructList(params,false,callback);
  }
  /**
   * 初始化方法
   * @method initComment
   * @for InfoStore
   * @param {参数类型} {'pageindex':num, 'pagesize':num,  'taskId': string, 'id': string, } 参数说明 ;
   * @return {返回值类型}
   */
  @action
  initComment(params){
    return this.getInstructListComment(params);
  }
  //滚动到底部时候，一般用于加载下一页数据。
  @action
  onEndReached(event){
    const {loading,hasMore,current,id,pageSize} = this.listViewStore;
    if (loading || !hasMore) {
      return;
    }
    const params = {'pageindex': current+1,'pagesize':pageSize,'taskId':id};
    this.getInstructList(params);
  }
  //下拉刷新的回调，一般用于加载第一页数据
  @action
  onRefresh(event){
    const {loading,id,pageSize} = this.listViewStore;
    if (loading) {
      return;
    }
    const params = {'pageindex': 1,'pagesize':pageSize,'taskId':id};
    this.getInstructList(params,true);
  }
  //设置当前回复id
  @action
  setInstructTopid(instructTopid=""){
    this.instructTopid = instructTopid;
  }
  @action
  setInstructid(instructid=""){
    this.instructid = instructid;
  }
  /**
   * 获取指导意见列数据
   * @method getInstructList
   * @for InfoStore
   * @param {参数类型} {'pageindex':num, 'pagesize':num,  'taskId': 'num' } 参数说明 ;
   * @return {返回值类型} {}·
   */
   @action
   getInstructList(params,refresh = false,callback=false){
     this.listViewStore.refreshLoading = !!refresh;
     if ('pageindex' in params) this.listViewStore.current = params.pageindex;
     const newParams = {
       ...params,
       'pageindex': params.pageindex || this.listViewStore.current,
       'pagesize': params.pagesize || this.listViewStore.pageSize,
     }
     if (!refresh && !this.listViewStore.hasMore) { return this.listViewStore; }
      Apis.getInstructList(newParams).then((data) => {
          let {discussList} = data;
          const {pageindex,pagesize,taskId} = newParams;
          const newState = {
            current:pageindex,
            data:[...(refresh ? [] : this.listViewStore.data), ...discussList],
            hasMore:discussList.length >= pagesize,
            isTable: false,
            loading: false,
            pageSize: pagesize,
            id:taskId,
            refreshLoading: false,
            template:{
              default: '<div></div>' +
              '<div></div>' +
              '<div></div>' +
              '<div></div>'
                  }
          };
          this.listViewStore.setState(newState);
          this.listViewStore.updateDataSource();
          callback?callback(data.showReportButton):null;
      });
   }
   //获取评论child数据
   @action
   getInstructListComment(params,refresh = false){
     this.listViewCommentStore.refreshLoading = !!refresh;
     if ('pageindex' in params) this.listViewCommentStore.current = params.pageindex;
     const newParams = {
       ...params,
       'pageindex': params.pageindex || this.listViewCommentStore.current,
       'pagesize': params.pagesize || this.listViewCommentStore.pageSize,
     }
     if (!refresh && !this.listViewCommentStore.hasMore) {return this.listViewCommentStore; }
     //console.log('newParams',newParams);
      return Apis.getInstructList(newParams).then((data) => {
          const {pageindex,pagesize,taskId,id} = newParams;
          let discussListMain = data.discussList.find((item,index)=>{
            return  item.id === id ;
           });
           let rreply = {};//被回复的评论
           rreply = toJS(discussListMain.instructReplyList);
           let {instructReplyList,...instructTop} = discussListMain;//某id的主体评论或汇报
          const newState = {
            current:pageindex,
            data:[...(refresh ? [] : this.listViewCommentStore.data), ...rreply],
            hasMore:rreply.length >= pagesize,
            isTable: false,
            loading: false,
            pageSize: pagesize,
            id:taskId,
            refreshLoading: false,
            template:{
              default: '<div></div>' +
              '<div></div>' +
              '<div></div>' +
              '<div></div>'
                  }
          };
          this.listViewCommentStore.setState(newState);
          this.listViewCommentStore.updateDataSource();
          return Promise.resolve(instructTop);
      });
   }
   //清空
   @action
   clearState(callback){
    this.listViewStore.clearData();
    this.listViewCommentStore.clearData();
   }
}
